﻿using MediatR;
using MyRabbit.Application.Command;
using MyRabbit.Application.ErrorCode;
using MyRabbit.Domain;
using MyRabbit.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRabbit.Application.CommandHandler
{
    public class BreedUpdateCommandHandler : IRequestHandler<BreedUpdateCommand, APIResult<object>>
    {

        /// <summary>
        /// 养殖仓储
        /// </summary>
        private readonly IRepository<Breed> breedRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="breedRep"></param>

        public BreedUpdateCommandHandler(IRepository<Breed> breedRep)
        {
            this.breedRep = breedRep;
        }

        public async Task<APIResult<object>> Handle(BreedUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = Codes.修改成功;
            result.Message = Codes.修改成功.ToString();
            Breed breed = await breedRep.GetByIdAsync(request.BreedId);
            if (breed == null)
            {
                result.Code = Codes.修改失败;
                result.Message = Codes.修改失败.ToString() + "养殖信息不存在";
                return await Task.FromResult(result);
            }
            else
            {
                breed.Variety = request.Variety;
                breed.Source = request.Source;
                breed.FeedingDays = request.FeedingDays;
                breed.FeedingBrand = request.FeedingBrand;
                breed.FeedingName = request.FeedingName;
                breed.SaleDate = request.SaleDate;
                await breedRep.UpdateAsync(breed);
            }
            return await Task.FromResult(result);
        }
    }
}
